<template>
	<view>
		<u-navbar title="个人信息" :background="{ background: '#a0cfff' }" title-color="#FFFFFF"></u-navbar>
		<view class="flex-ct">
			<view class="u-m-t-50 u-m-b-50">
				<u-upload :action="action" :file-list="fileList" max-count="1" :show-progress="false"
					v-if="!uploadShow" @on-remove="onRemove" @on-Success="onSuccess">
				</u-upload>
				<u-avatar :src="src" size="213" v-else></u-avatar>
				<view class="u-m-t-20" style="text-align: center;color: #F08080;" @click="imageClick()">更新头像</view>
			</view>
		</view>

		<view class="u-m-30">
			<u-form :model="form" ref="uForm" label-width="150">
				<u-form-item label="账号编号">
					<u-input v-model="form.id" disabled />
				</u-form-item>
				<u-form-item label="姓名">
					<u-input v-model="form.username" />
				</u-form-item>
				<u-form-item label="邮箱">
					<u-input v-model="form.email" />
				</u-form-item>
				<u-form-item label="账号权限">
					<u-input v-model="form.role" disabled />
				</u-form-item>
			</u-form>
		</view>

		<view class="u-m-30">
			<u-button type="error" @click="getId()">修改信息</u-button>
		</view>
	</view>
</template>

<script>
	import helper from '../../common/helper.js'
	export default {
		data() {
			return {
				action: 'http://localhost:9000/eduoss/imgFile',
				fileList: [],
				fileImage: [],
				src: '',
				uploadShow: true,
				form: {
					id: "",
					username: "",
					email: '',
					role: ''
				}
			}
		},
		created() {
			this.getUser()
		},
		methods: {
			getUser() {
				let that = this
				let user = uni.getStorageSync("user")
				that.src = user.userUrl
				that.form.email = user.email
				that.form.username = user.username
				that.form.id = user.id
				if (user.role == 1) {
					that.form.role = "超级管理员"
				} else if (user.role == 2) {
					that.form.role = "管理员"
				} else if (user.role == 3) {
					that.form.role = "普通用户"
				}
			},
			imageClick() {
				let that = this
				that.uploadShow = !that.uploadShow
			},
			getId() {
				let that = this
				let user = uni.getStorageSync("user")
				var file = []
				console.log(that.fileImage.length)
				if (that.fileImage.length == 0) {
					file.push(that.src)
				} else {
					that.fileImage.forEach((item, index) => {
						file.push(item.url)
					})
				}

				let data = {
					id: user.id,
					username: that.form.username,
					email: that.form.email,
					userUrl: file.join("")
				}
				that.postRequest(helper.getId, data).then(res => {
					if (res.data.flag == "200") {
						let serve = res.data.user
						uni.setStorage({
							key: "user",
							data: serve
						})
						that.getUser()
						uni.showToast({
							title: '修改成功',
							icon: 'none',
							duration: 2000
						})
					} else {
						uni.showToast({
							title: '修改失败',
							icon: 'none',
							duration: 2000
						})
					}
				})
			},
			onSuccess(data, index, lists) {
				const that = this
				let currentFile = [];
				// console.log(data,lists)
				lists.forEach((item, index) => {
					if (item.response) {
						let obj = {
							url: item.response.collectNum
						}
						currentFile.push(obj)
					} else {
						let link = item.url
						let obj = {
							url: link
						}
						currentFile.push(obj)
					}
				})
				that.fileImage = currentFile
			},
			onRemove(data, lists) {
				const that = this
				if (lists.length > 0) {
					var currentfileList = [];
					lists.forEach((item, index) => {
						if (item.response) {
							let obj = {
								url: item.response.collectNum
							}
							currentfileList.push(obj)
						} else {
							let link = item.url
							let obj = {
								url: link
							}
							currentfileList.push(obj)
						}
					})
					that.fileImage = currentfileList
				} else {
					that.fileImage = []
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	/deep/ .u-preview-wrap.data-v-49deb6f2 {
		border: 0rpx solid red;
	}
</style>
